import {
  qqmap
} from '../../../utils/qqmap'

Page({
  data: {
    // 小区列表数据
    streetList: [],
    // 用户所在地址
    address: ''
  },
  onLoad() {
    this.getLocation()
  },
  async getLocation() {
    const location = await wx.getLocation()
    console.log(location)
    this.getStreetList(location)
    this.getUserAddress(location)
  },
  // 获取周边小区
  getStreetList(location) {
    // 使用腾讯地图，根据经纬度，获取周边小区
    qqmap.search({
      keyword: '住宅小区', //搜索关键词
      //设置周边搜索中心点
      location: [location.latitude, location.longitude].join(','),
      page_size: 5,
      success: (res) => {
        console.log('qqmap sdk 返回数据', res)
        const streetList = res.data.map(item => {
          return {
            id: item.id,
            title: item.title,
            distance: item._distance
          }
        })
        // 设置页面数据
        this.setData({
          streetList
        })
      }
    })
  },
  // 获取用户所在地址
  getUserAddress(location) {
    qqmap.reverseGeocoder({
      location: [location.latitude, location.longitude].join(','),
      success: (res) => {
        console.log('用户所在地址', res)
        this.setData({
          address: res.result.address
        })
      }
    })
  },
  // 重新定位事件处理函数
  async handleChooseLocationTap(e) {
    const location = await wx.chooseLocation()
    console.log(location)
    // 重置小区和所在地信息数据
    this.getStreetList(location)
    this.getUserAddress(location)
  }
})